﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace Sustainalytics.Caching.Tests
{
    [TestClass]
    public class IAppCacheExtensionsTests
    {
        protected IAppCache Cache { get; set; }


        [TestInitialize]
        public void TestInitialize()
        {
            this.Cache = new AppMemoryCache();
            this.Cache.Clear();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            this.Cache.Clear();
        }


        [TestMethod]
        [ExpectedException(typeof(CacheItemKeyNullOrEmptyException))]
        public void Get_Bytes_KeyNull_Exception()
        {
            this.Cache.GetBytes(null);
        }

        [TestMethod]
        [ExpectedException(typeof(CacheItemKeyNullOrEmptyException))]
        public void Set_Bytes_KeyNull_Exception()
        {
            this.Cache.Set(null, new byte[] { 1, 2, 3 });
        }

        [TestMethod]
        public void Set_Bytes_ValueNull_Success()
        {
            this.Cache.Set("123", (byte[])null);
        }

        [TestMethod]
        public void Get_Set_Bytes()
        {
            string key = "1";
            byte[] expectedValue = new byte[] { 1, 2, 3, 4 ,5 };

            this.Cache.Set(key, expectedValue);

            byte[] value = this.Cache.GetBytes(key);

            Assert.IsNotNull(value, "Retrieved bytes are null.");
            Assert.IsTrue(expectedValue.SequenceEqual(value), "Retrieved bytes are different than the expected set.");
        }
    }
}
